package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.DevicesManagerAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.DevicesUndeMettingAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.DevicesManagerBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.DevicesUndeMettingBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.custom_view.MyRecyclerView;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.inter.IonClickedItemListenter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IDevicesUndeMettingContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.DevicesUndeMettingModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.RecyclerViewUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/9.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class DevicesUndeMettingPersenter implements IDevicesUndeMettingContract.IDevicesUndeMettingPersenter {
    private IDevicesUndeMettingContract.IDevicesUndeMettingView mIDevicesUndeMettingView;
    private DevicesUndeMettingAdapter mDevicesUndeMettingAdapter;
    private IDevicesUndeMettingContract.IDevicesUndeMettingModle mDevicesUndeMettingModle;
    private String TAG = DevicesUndeMettingPersenter.class.getSimpleName();
    private Handler handler = new Handler();
    private List<DevicesUndeMettingBean> mlist;
    private RecyclerView myRecyclerView;

    public DevicesUndeMettingPersenter(IDevicesUndeMettingContract.IDevicesUndeMettingView mIDevicesUndeMettingView) {
        this.mIDevicesUndeMettingView = mIDevicesUndeMettingView;
        mDevicesUndeMettingModle = new DevicesUndeMettingModle();
    }


    @Override
    public void initTitle() {
        mIDevicesUndeMettingView.setTitle();
    }

    @SuppressLint("LongLogTag")
    @Override
    public void initRecyclerView(MyRecyclerView mMyRecyclerView, Context mContext) {
        myRecyclerView = mMyRecyclerView;
        RecyclerViewUtils mRecyclerViewUtils =   new RecyclerViewUtils();
        mDevicesUndeMettingAdapter = new DevicesUndeMettingAdapter<DevicesUndeMettingBean>(mContext);
        mRecyclerViewUtils.initView(mMyRecyclerView,0,mContext, mDevicesUndeMettingAdapter);
        setDevicesManagerAdapterListener(mDevicesUndeMettingAdapter);
    }

    @Override
    public void loadDate(Context mContext, String id) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.DEVICES_UNDE_MANAGE_LIST)
                .addHeader("sessionId",mDevicesUndeMettingModle.getToken(mContext))
                .addParams("id", id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                       // Log.e(TAG, "loadRecyclerViewDate:token ==sdg===> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                    String normalDate = JsonUitl.getStringDateFeld(object);
                                    Type type = new TypeToken<List<DevicesUndeMettingBean>>(){}.getType();
                                    mlist = new Gson().fromJson(normalDate,type);
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            mDevicesUndeMettingAdapter.AddItemDate(mlist);
                                        }
                                    });
                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 设置监听
     * */
    private void setDevicesManagerAdapterListener(DevicesUndeMettingAdapter managerAdapter) {
        managerAdapter.setmIonClickedItemListenter(new IonClickedItemListenter() {
            @Override
            public void onClickedItem(View view) {
               int postion =  myRecyclerView.getChildLayoutPosition(view);
                DevicesUndeMettingBean bean = mlist.get(postion);
                if(bean != null){
                  String id =   bean.getId();
                  if(id != null && !TextUtils.isEmpty(id)){
                     // Log.e(TAG, "loadRecyclerViewDate:token ==id111===> "+id);
                      mIDevicesUndeMettingView.toNextPager(id);
                  }
                }

            }

            @Override
            public void onClickedFist(int postion) {

            }

            @Override
            public void onClickedSecond(int postion) {

            }
        });
    }
}
